package com.vimi8.ebb.api.datareport;

import com.vimi8.ebb.configuration.HttpResponseEntity;
import com.vimi8.ebb.dto.TrainInfoCountDto;
import com.vimi8.ebb.dto.TrainInfoDataRepport;
import com.vimi8.ebb.dto.TrainInfoTotalDto;
import com.vimi8.ebb.model.Error;
import com.vimi8.ebb.model.GeoCodings;
import com.vimi8.ebb.model.Paginator;
import com.vimi8.ebb.model.TrainInfo;
import com.vimi8.ebb.service.GeoCodingsService;
import com.vimi8.ebb.service.TrainInfoService;
import com.vimi8.ebb.service.TrainMemberService;
import com.vimi8.ebb.util.PagesUtil;
import com.vimi8.ebb.util.SubStringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by think on 2016/12/5.
 */
@RestController
@RequestMapping("/data/report")
public class TrainDataReportController
{
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TrainInfoService trainInfoService;
    @Autowired
    private GeoCodingsService geoCodingsService;
    @Autowired
    private TrainMemberService trainMemberService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;



    @ApiOperation(value = "查询数据报告培训统计信息",notes = "查询数据报告培训统计信息",response = String.class,tags = {"数据报告"})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功,返回影响行数", response = String.class),
            @ApiResponse(code = 500, message = "失败,返回消息", response = String.class) })
    @RequestMapping(value = "/train/",method = RequestMethod.GET)
    public HttpResponseEntity<TrainInfoDataRepport> getFrontTrainInfoDataReport(@ApiParam(value = "year",required = true) @RequestParam(value="year",required = true)int year,
                                                                                @ApiParam(value = "quarter",required = true) @RequestParam(value="quarter",required = true)int quarter) {
        String startTtime=null;
        String endTtime=null;
        TrainInfoDataRepport trainInfoDataRepport=new TrainInfoDataRepport();
        String geoCode = SubStringUtil.separateGeoCode("520600000000","3");
        logger.info("======================================code:" + geoCode);
        List<GeoCodings> geoCodingses = geoCodingsService.getGeoCodingsByCode("520600000000",geoCode);
        if (null!=geoCodingses){
            trainInfoDataRepport.setGeoCodeList(geoCodingses);
        }
        if(1==quarter){
            startTtime=year+"-01";
            endTtime=year+"-04";
        }
        if(2==quarter){
            startTtime=year+"-04";
            endTtime=year+"-07";
        }
        if(3==quarter){
            startTtime=year+"-07";
            endTtime=year+"-10";
        }
        if(4==quarter){
            startTtime=year+"-10";
            endTtime=(year+1)+"-01";
        }
          List<TrainInfoCountDto> trainInfoCountDtoList=new ArrayList<TrainInfoCountDto>();

        for(GeoCodings geoCodings:geoCodingses){
            if(2017==year&&1==quarter){
                //如果是2017年第一季度 兼容以前的数据
                startTtime=null;
                TrainInfoCountDto trainInfoCountDto=  trainInfoService.queryTrainPeoplesTotalForReport(startTtime,endTtime,geoCodings.getId());
                trainInfoCountDtoList.add(trainInfoCountDto);
            }else{
                TrainInfoCountDto trainInfoCountDto=  trainInfoService.queryTrainGroupByMonthForReport(startTtime,endTtime,geoCodings.getId());
                trainInfoCountDtoList.add(trainInfoCountDto);
            }

        }
        trainInfoDataRepport.setTrainInfoCountDtos(trainInfoCountDtoList);
        return new HttpResponseEntity<TrainInfoDataRepport>(trainInfoDataRepport,HttpStatus.OK);
    }

}
